<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\BusinessMember;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;

class BusinessMemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BusinessMember(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('nickname');
//            $grid->column('password');
            $grid->column('company_name');
            $grid->column('company_leader');
            $grid->column('company_tel');
//            $grid->column('level');
//            $grid->column('parent_id');
//            $grid->column('member_status');
            $grid->column('phone');
            $grid->column('email');
//            $grid->column('wx_applet_openid');
            $grid->column('login_ip');
            $grid->column('login_time');
            $grid->column('remark');
//            $grid->column('created_by');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BusinessMember(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('nickname');
            $show->field('password');
            $show->field('company_name');
            $show->field('company_leader');
            $show->field('company_tel');
            $show->field('level');
            $show->field('parent_id');
            $show->field('member_status');
            $show->field('phone');
            $show->field('email');
            $show->field('wx_applet_openid');
            $show->field('login_ip');
            $show->field('login_time');
            $show->field('remark');
            $show->field('created_by');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BusinessMember(), function (Form $form) {
            $form->display('id');
            $form->text('username')->rules('required')->help('登录用');
            $form->text('nickname')->help('默认为用户名');
            $form->password('password')->rules('required');
            $form->text('company_name');
            $form->text('company_leader');
            $form->text('company_tel');
//            $form->text('level');
//            $form->text('parent_id');
            $form->radio('member_status')->options([
                0 => '禁用',
                1 => '启用',
            ])
            ->default(1);
            $form->text('phone');
            $form->text('email');
            $form->textarea('remark')->rules('max:100');

            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                // 密码加密
                if ($form->password && $form->model()->password!= $form->password) {
                    $form->password = Hash::make($form->password);
                }

                // 昵称处理
                if (!$form->nickname) {
                    $form->nickname = $form->username;
                }

                // 后台添加时候，默认设置用户等级为品牌商
                $form->level = 1;
            });
        });
    }

    // 品牌方用户获取
    public function getMembers(Request $request)
    {
        $q = $request->get('q');
        return \App\Models\BusinessMember::where('username', 'like', "%$q%")
            ->where('position_id', '=', 1) // 只查询品牌方账号
            ->paginate(null, ['id', 'username as text']);
    }
}
